﻿using System;
using System.Data;
using Microsoft.ApplicationBlocks.Data;
using viNews.Providers;
using visinia.SmartEngine.Providers;

namespace viNews
{
    public class SqlDataProvider : viNewsDataProvider
    {
        #region INIT
        private static string _sqlConnectionString = null;

        public SqlDataProvider()
        {
            _sqlConnectionString = ProviderController.GetDataProviderConnectionString();
        }
        public string ConnectionString
        {
            get { return _sqlConnectionString; }
        }
        #endregion

        #region CATEGORIES
        public override IDataReader GetCategories()
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viNewsGetCategories");
        }

        public override void AddCategory(string Name, int ParentId, int PageId, int ContentPageId)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "viNewsAddCategory", Name, ParentId, PageId, ContentPageId);
        }

        public override void UpdateCategory(int CategoryId, string Name, int ParentId, int PageId, int ContentPageId)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "viNewsUpdateCategory", CategoryId, Name, ParentId, PageId, ContentPageId);
        }

        public override void DeleteCategory(int CategoryId)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "viNewsDeleteCategory", CategoryId);
        }
        #endregion

        #region NEWS
        public override IDataReader AddNews(string title, DateTime Date, int category, string imageurl, string author, string summary, bool draft, string url, string description)
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viNewsAddNews", title, Date, category, imageurl, author, summary, draft, url, description);
        }

        public override void DeleteNews(int NewsId)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "viNewsDeleteNews", NewsId);
        }

        public override void UpdateNews(int NewsId, string title, DateTime date, int category, string imageurl, string author, string summary, bool draft, string url, string description)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "viNewsUpdateNews", NewsId, title, date, category, imageurl, author, summary, draft, url, description);
        }

        public override IDataReader GetNews(int NewsId)
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viNewsGetNews", NewsId);
        }
        public override IDataReader GetNews(int start, int count, bool PublishOnly)
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viNewsGetNewsList", start, count, PublishOnly);
        }
        public override IDataReader GetNewsCount()
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viNewsGetNewsCount");
        }
        public override IDataReader GetNewsDescription(int NewsId)
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viNewsGetNewsDescription", NewsId);
        }
        public override IDataReader GetNewsByCategory(int Category, int Count)
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viNewsGetNewsByCategory", Category, Count);
        }

        public override IDataReader GetNewsByTag(int Tag, int Count)
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viNewsGetNewsByTag", Tag, Count);
        }
        public override IDataReader GetNewsCountByCategory(int CategoryId)
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viNewsGetNewsCountByCategory", CategoryId);
        }

        public override IDataReader GetNewsCountByTag(int TagId)
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viNewsGetNewsCountByTag", TagId);
        }
        #endregion

        #region TAGS
        public override IDataReader GetTags()
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viNewsGetTags");
        }
        public override IDataReader GetTags(int NewsId)
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viNewsGetTagsByNewsId", NewsId);
        }
        public override void InsertTag(int TagId, int NewsId)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "viNewsInsertTag", TagId, NewsId);
        }
        public override void RemoveTag(int TagId, int NewsId)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "viNewsRemoveTag", TagId, NewsId);
        }
        public override IDataReader AddTag(string Name)
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viNewsAddTag", Name);
        }

        public override void DeleteTag(int TagId)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "viNewsDeleteTag", TagId);
        }
        #endregion

        #region DATAFILTERS
        public override IDataReader GetFilters()
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viNewsGetFilters");
        }
        public override void AddFilter(int FilterType, int ModuleId, string Data, int ItemsCount)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "viNewsAddFilter", FilterType, ModuleId, Data, ItemsCount);
        }
        public override void UpdateFilter(int FilterId, int FilterType, int ModuleId, string Data, int ItemsCount)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "viNewsUpdateFilter", FilterId, FilterType, ModuleId, Data, ItemsCount);
        }
        public override void DeleteFilter(int FilterId)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "viNewsDeleteFilter", FilterId);
        }
        #endregion

        #region BASE SETTINGS
        public override void AddBaseSetting(string key, string value)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "viNewsAddBaseSetting", key, value);
        }

        public override void DeleteBaseSetting(string key)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "viNewsDeleteBaseSetting", key);
        }

        public override void UpdateBaseSetting(string key, string value)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "viNewsUpdateBaseSetting", key, value);
        }

        public override IDataReader GetBaseSettings()
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viNewsGetBaseSettings");
        }
        #endregion

        #region COMMENTS
        public override IDataReader GetComments(int NewsId)
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viNewsGetComments", NewsId);
        }
        public override IDataReader GetRecentComments(int Count)
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viNewsGetRecentComments", Count);
        }
        public override IDataReader GetUnapprovedComments()
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viNewsGetUnapprovedComments");
        }
        public override IDataReader GetApprovedComments()
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viNewsGetApprovedComments");
        }
        public override IDataReader GetCommentsCount()
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viNewsGetCommentsCount");
        }
        public override void ApproveComment(int CommentId)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "viNewsApproveComment", CommentId);
        }

        public override IDataReader AddComment(int NewsId, string Author, string Email, string Website, DateTime Date, string Content, bool IsApproved)
        {
            return SqlHelper.ExecuteReader(ConnectionString, "viNewsAddComment", NewsId, Author, Email, Website, Date, Content, IsApproved);
        }

        public override void DeleteComment(int CommentId)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "viNewsDeleteComment", CommentId);
        }

        public override void DeleteComments(int PostId)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, "viNewsDeleteComments", PostId);
        }
        #endregion
    }
}
